package com.ruoyi.medicine.service.impl;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.uuid.IdUtils;
import com.ruoyi.medicine.domain.*;
import com.ruoyi.medicine.mapper.*;
import com.ruoyi.medicine.service.IHisMedicineOrderService;
import com.ruoyi.system.mapper.SysUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 订单信息Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-08-30
 */
@Service
public class HisMedicineOrderServiceImpl implements IHisMedicineOrderService
{
    @Autowired
    private HisMedicineOrderMapper hisMedicineOrderMapper;
    @Autowired
    private HisMedicineDrugMapper hisMedicineDrugMapper;
    @Autowired
    private HisMedicineOrderDetailMapper hisMedicineOrderDetailMapper;
    @Autowired
    private HisMedicineCartMapper hisMedicineCartMapper;
    @Autowired
    private SysUserMapper sysUserMapper;
    @Autowired
    private HisMedicineOrderAuditMapper hisMedicineOrderAuditMapper;

    /**
     * 查询订单信息
     *
     * @return 订单信息
     */
    @Override
    public HisMedicineOrder selectHisMedicineOrderByOrderId(HisMedicineOrder hisMedicineOrder)
    {
        System.out.println(hisMedicineOrder.getOrderId());
        HisMedicineOrder hisMedicineOrder1 = hisMedicineOrderMapper.selectHisMedicineOrderByOrderId(hisMedicineOrder.getOrderId());
        System.out.println(hisMedicineOrder1);
        //查询详情
        HisMedicineOrderDetail hisMedicineOrderDetail = new HisMedicineOrderDetail();
        hisMedicineOrderDetail.setDrugName(hisMedicineOrder.getDrugName());
        hisMedicineOrderDetail.setOrderId(hisMedicineOrder1.getOrderId());
        List<HisMedicineOrderDetail> hisMedicineOrderDetails = hisMedicineOrderDetailMapper.selectHisMedicineOrderDetailList(hisMedicineOrderDetail);
        for (HisMedicineOrderDetail medicineOrderDetail : hisMedicineOrderDetails) {
            //查询每个详情的药品
            HisMedicineDrug hisMedicineDrug = hisMedicineDrugMapper.selectHisMedicineDrugByDrugId(medicineOrderDetail.getDrugId());
            medicineOrderDetail.setHisMedicineDrug(hisMedicineDrug);
        }
        hisMedicineOrder1.setHisMedicineOrderDetailList(hisMedicineOrderDetails);
        return hisMedicineOrder1;
    }
    @Override
    public HisMedicineOrder selectHisMedicineOrderByOrderId(Long orderId)
    {

        return  hisMedicineOrderMapper.selectHisMedicineOrderByOrderId(orderId);
    }

    /**
     * 查询订单信息列表
     * 
     * @param hisMedicineOrder 订单信息
     * @return 订单信息
     */
    @Override
    public List<HisMedicineOrder> selectHisMedicineOrderList(HisMedicineOrder hisMedicineOrder)
    {
        return hisMedicineOrderMapper.selectHisMedicineOrderList(hisMedicineOrder);
    }

    /**
     * 新增订单信息
     *
     * @return 结果
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
    public int insertHisMedicineOrder(Map<String,Object> map) throws JsonProcessingException {
        //获取采购用户信息
        SysUser user = sysUserMapper.selectUserById(Long.parseLong(map.get("userId").toString()));
        ObjectMapper objectMapper = new ObjectMapper();
        //添加订单
        HisMedicineCart hisMedicineCart = hisMedicineCartMapper.selectMedicineCartId(user.getUserId());
        String cart = hisMedicineCart.getCart();//获取购物车
        Map<Long, HisMedicineDrug> carMap = objectMapper.readValue(cart, new TypeReference<Map<Long, HisMedicineDrug>>() {});
        ArrayList<Integer> durgIds = (ArrayList<Integer>) map.get("durgId");
        //保存订单
        HisMedicineOrder hisMedicineOrder = new HisMedicineOrder();
        hisMedicineOrder.setOrderNum(IdUtils.randomUUID());
        hisMedicineOrder.setOrderPerson(hisMedicineCart.getUserId());
        hisMedicineOrder.setOrderStatus("0");
        hisMedicineOrder.setCreateTime(new Date());
        hisMedicineOrder.setCreateBy(user.getUserName());
        hisMedicineOrder.setSupplierId(Long.parseLong(map.get("supplierId").toString()));
        hisMedicineOrder.setRemark(map.get("remark").toString());
        int i = hisMedicineOrderMapper.insertHisMedicineOrder(hisMedicineOrder);
        //添加订单详情
        durgIds.forEach(o->{
            //获取购物车药品
            HisMedicineDrug hisMedicineDrug = carMap.get(Long.parseLong(o.toString()));
            //存放到订单详情
            HisMedicineOrderDetail hisMedicineOrderDetail = new HisMedicineOrderDetail();
            hisMedicineOrderDetail.setOrderId(hisMedicineOrder.getOrderId());
            hisMedicineOrderDetail.setDetailNum(Long.parseLong(hisMedicineDrug.getCarNum().toString()));
            hisMedicineOrderDetail.setCreateTime(new Date());
            hisMedicineOrderDetail.setCreateBy(user.getUserName());
            hisMedicineOrderDetail.setDrugId(hisMedicineDrug.getDrugId());
            hisMedicineOrderDetailMapper.insertHisMedicineOrderDetail(hisMedicineOrderDetail);
            //删除购物车订单
            carMap.remove(Long.parseLong(o.toString()));
        });

        //修改购物车
        String s = objectMapper.writeValueAsString(carMap);
        hisMedicineCart.setCart(s);
        hisMedicineCartMapper.updateMedicineCart(hisMedicineCart);

        return i;
    }

    /**
     * 修改订单信息
     * 
     * @param hisMedicineOrder 订单信息
     * @return 结果
     */
    @Override
    @Transactional
    public int updateHisMedicineOrder(HisMedicineOrder hisMedicineOrder,Long userId)
    {
        hisMedicineOrder.setUpdateTime(DateUtils.getNowDate());
        //添加审核记录
        HisMedicineOrderAudit hisMedicineOrderAudit = new HisMedicineOrderAudit();
        hisMedicineOrderAudit.setOrderId(hisMedicineOrder.getOrderId());
        hisMedicineOrderAudit.setAuditPerson(userId);
        hisMedicineOrderAudit.setAuditResult(hisMedicineOrder.getOrderStatus());
        hisMedicineOrderAudit.setAuditContent(hisMedicineOrder.getRemark());
        hisMedicineOrderAudit.setStatus("0");
        hisMedicineOrderAudit.setCreateTime(new Date());
        hisMedicineOrderAuditMapper.insertHisMedicineOrderAudit(hisMedicineOrderAudit);
        //修改审核状态
        return hisMedicineOrderMapper.updateHisMedicineOrder(hisMedicineOrder);
    }

    @Override
    @Transactional
    public int updateHisMedicineOrder(HisMedicineOrder hisMedicineOrder)
    {
        //修改审核状态
        return hisMedicineOrderMapper.updateHisMedicineOrder(hisMedicineOrder);
    }

    /**
     * 批量删除订单信息
     * 
     * @param orderIds 需要删除的订单信息主键
     * @return 结果
     */
    @Override
    public int deleteHisMedicineOrderByOrderIds(Long[] orderIds)
    {
        return hisMedicineOrderMapper.deleteHisMedicineOrderByOrderIds(orderIds);
    }

    /**
     * 删除订单信息信息
     * 
     * @param orderId 订单信息主键
     * @return 结果
     */
    @Override
    public int deleteHisMedicineOrderByOrderId(Long orderId)
    {
        return hisMedicineOrderMapper.deleteHisMedicineOrderByOrderId(orderId);
    }
}
